<html>
<head><meta charset="utf-8"><title>Feature based type defaults · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Feature.20based.20type.20defaults.html">Feature based type defaults</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="222217367"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Feature%20based%20type%20defaults/near/222217367" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Feature.20based.20type.20defaults.html#222217367">(Jan 10 2021 at 11:07)</a>:</h4>
<p>Hey, is there a way to have a type default that’s dependent on features?</p>
<p>For example if I have <code>struct A&lt;T=B&gt;(T);</code> Is there a way for <code>T</code> have a default when the <code>b</code> feature is enabled, and have no default when that feature is disabled without redefining the whole type?</p>



<a name="222225742"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Feature%20based%20type%20defaults/near/222225742" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Eric Huss <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Feature.20based.20type.20defaults.html#222225742">(Jan 10 2021 at 14:56)</a>:</h4>
<p>Type parameters can take attributes like this:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">struct</span> <span class="nc">S</span><span class="o">&lt;</span><span class="w"></span>
<span class="w">    </span><span class="cp">#[cfg(feature = </span><span class="s">"foo"</span><span class="cp">)]</span><span class="w"> </span><span class="n">T</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kt">i32</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="cp">#[cfg(not(feature = </span><span class="s">"foo"</span><span class="cp">))]</span><span class="w"> </span><span class="n">T</span><span class="w"></span>
<span class="o">&gt;</span><span class="p">(</span><span class="n">T</span><span class="p">);</span><span class="w"></span>
</code></pre></div>
<p>I don't think it can be simplified much more than that.</p>



<a name="223167808"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Feature%20based%20type%20defaults/near/223167808" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Feature.20based.20type.20defaults.html#223167808">(Jan 18 2021 at 22:04)</a>:</h4>
<p><span class="user-mention" data-user-id="120518">@Eric Huss</span> At least it’s possible to do without redefining the whole type, thanks for your response!</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>